//
//  HMStatusToolBar.swift
//  weibo
//
//  Created by LCH on 16/3/10.
//  Copyright © 2016年 lch. All rights reserved.
//

import UIKit

class HMStatusToolBar: UIView {
    
    // TODO:隐式可选的概念
    private var retweetButton: UIButton!
    private var commentButton: UIButton!
    private var unlikeButton: UIButton!
    // TODO在cell赋值的时候嵌套ViewModel,init,didSet赋值无效
    var statusViewModel:HMStatusViewModel? {
    
        didSet {
        
         retweetButton.setTitle(statusViewModel?.repostsCountStr, forState:UIControlState.Normal)
         commentButton.setTitle(statusViewModel?.commentsCountStr, forState: UIControlState.Normal)
         unlikeButton.setTitle(statusViewModel?.attitudesCountStr, forState: UIControlState.Normal)
            
        
        }
    
    
    }
    
    
    
    

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
    
        retweetButton = addChildButton("timeline_icon_retweet", title: "转发")
        commentButton = addChildButton("timeline_icon_comment", title: "评论")
         unlikeButton = addChildButton("timeline_icon_unlike", title: "赞")

        let sp1 = addSpliteView()
        let sp2 = addSpliteView()
        
        
        retweetButton.snp_makeConstraints { (make) -> Void in
            
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(commentButton)
        }
        commentButton.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(retweetButton)
            make.width.equalTo(unlikeButton)
            make.leading.equalTo(retweetButton.snp_trailing)
            
        }
        unlikeButton.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(commentButton)
            make.trailing.equalTo(self)
            make.leading.equalTo(commentButton.snp_trailing)
        }
        
        sp1.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(retweetButton.snp_trailing)
            make.centerY.equalTo(self)
        }
        sp2.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(commentButton.snp_trailing)
            make.centerY.equalTo(self)
        }
        
    
    }
    
    
    
    
    private func addSpliteView() -> UIView {
    
    let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
    addSubview(imageView)
    
    return imageView
    }
    
   
    
    
    private func addChildButton(imageNamed:String, title: String) -> UIButton {
    
    let button = UIButton()
    button.setTitle(title, forState: UIControlState.Normal)
    button.setImage(UIImage(named: imageNamed), forState:UIControlState.Normal)
    button.titleLabel?.font = UIFont.systemFontOfSize(14)
    button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
    
    button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState:UIControlState.Highlighted )
    button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        
    addSubview(button)
    return button
    }
    
}
